package com.orangetentacle.bcblackpool;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.content.Context;
import android.util.Log;

public class RSSHandler extends DefaultHandler {
	 
    // Used to define what elements we are currently in
	private boolean _inTitle = false;
	private boolean _inContent = false;
	private boolean _inName = false;
	private boolean _inUrl = false;
	private boolean _inEntry = false;
	
    // Feed and Article objects to use for temporary storage
    private Tweet _currentTweet = new Tweet();
    public List<Tweet> _tweets = new ArrayList<Tweet>();
 
    // Number of articles added so far
    private int _articlesAdded = 0;
    // Number of articles to download
    private static final int ARTICLES_LIMIT = 20; 
 
    public void startElement(String uri, String name, String qName,
            Attributes atts) {
    	if (name.trim().equals("entry"))
    		_inEntry = true;
    	
    	if (_inEntry) { 	
	        if (name.trim().equals("title"))
	            _inTitle = true;
	        else if (name.trim().equals("content"))
	            _inContent = true;
	        else if (name.trim().equals("name"))
	            _inName = true;
	        else if (name.trim().equals("link") && _currentTweet.url == null)
	        	_currentTweet.url = atts.getValue("href");
    	}
    }
 
    public void endElement(String uri, String name, String qName)
            throws SAXException {
        if (name.trim().equals("title"))
            _inTitle = false;
        else if (name.trim().equals("content"))
            _inContent = false;
        else if (name.trim().equals("name"))
            _inName = false;
        else if (name.trim().equals("link"))
        	_inUrl = false;
 
        // Check if tweet is complete.
        if (_currentTweet.name != null && _currentTweet.content != null 
        		&& _currentTweet.title != null && _currentTweet.url != null) {
        	Tweet newTweet = new Tweet();
        	
        	newTweet.id = _articlesAdded;
        	newTweet.title = _currentTweet.title;
        	newTweet.name = _currentTweet.name;
        	newTweet.content = _currentTweet.content;
        	newTweet.url = _currentTweet.url;
        	
        	_tweets.add(newTweet);
        	
        	_articlesAdded++;
        	_currentTweet = new Tweet();
        }
 
        // Lets check if we've hit our limit on number of articles
        if (_articlesAdded >= ARTICLES_LIMIT)
            throw new SAXException();
    }
    
    public void characters(char ch[], int start, int length) {
    	 
        String chars = (new String(ch).substring(start, start + length));
 
        try {
            // If not in item, then title/link refers to feed
            if (_inEntry) {
                if (_inName)
                    _currentTweet.name = chars;
                if (_inContent)
                    _currentTweet.content = chars;
                if (_inTitle)
                    _currentTweet.title = chars;
            }
        } catch (Exception e) {
            Log.e("BCBlackpool", e.toString());
        }
 
    }    
 
    public void updateTweets(Context ctx) {
        try {
            URL url = new URL("http://search.twitter.com/search.atom?q=bcblackpool");
            
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();
            xr.setContentHandler(this);
            xr.parse(new InputSource(url.openStream()));
           
        }
        catch (Exception e) {
        	Log.e("BCBlackpool", e.toString());
        }
    }
 
}